/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doc2db.interfaces.action;

import doc2db.DbManager.DbSelects;
import doc2db.Keey;
import doc2db.UserProp;
import doc2db.fileWorker.FileBeginer;
import doc2db.fileWorker.FileWorker;
import doc2db.interfaces.mainWindow.MyFrame;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *save.png
 * @author yura
 */
public class NewBookmarksAction implements ActionListener {
    private JDialog d ;
    private MyFrame mf;
    private JLabel ln = new JLabel("название запроса : ");
    private JLabel li = new JLabel("запрос :");
    private JTextField name = new JTextField("name", 32);
    private JTextArea init = new JTextArea();
    private JButton ok = new JButton(new AbstractAction("<html><p>ok",
            new ImageIcon(FileBeginer.resurse_resurseFolderMenu+Keey.sp+"save.png")){
        public void actionPerformed(ActionEvent e) {
            UserProp up = new UserProp();
            DbSelects dBManager;
            try {
                dBManager = new DbSelects(up.getURL(), up.getUserProp());
                if(dBManager.sendQuery(init.getText())){
                    String fileName = FileBeginer.resurse_users_usersFolder +
                            Keey.sp + UserProp.user + Keey.sp + "bookmarks.ini";
                    if(name.getText().trim() != ""){
                        FileWorker.addNewProperty(fileName, name.getText().trim(),
                                init.getText().trim().replaceAll("\n", " "));
                        //mf.initMenuBar();
                        mf.menu.addbookMarks(name.getText().trim(), init.getText().trim().replaceAll("\n", " "));
                        d.setVisible(false);
                    }else{
                        JOptionPane.showConfirmDialog(null, "поле имени не должно быть пустым");
                    }
                }else {
                    JOptionPane.showConfirmDialog(null, "неправельнный запрос к БД");
                }
            } catch (SQLException ex) {
                JOptionPane.showConfirmDialog(null, "" + ex);
            } catch (ClassNotFoundException ex) {
                JOptionPane.showConfirmDialog(null, "" + ex);
            }
        }
    });
    private JButton cancel = new JButton(new AbstractAction("<html><p>отмена",
            new ImageIcon(FileBeginer.resurse_resurseFolderMenu+Keey.sp+"cancel.png")){
        public void actionPerformed(ActionEvent e) {
            d.setVisible(false);
        }
    });

    public NewBookmarksAction(MyFrame mf) {
        this.mf = mf;
    }



    private void initFrame(){
        d.setTitle("менеджер БД");
        d.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        d.setResizable(true);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screnSize = kit.getScreenSize();
        int screanHeight = screnSize.height ;
        int screanWidth = screnSize.width ;
        d.setSize(600, 380);
        d.setLocation((screanWidth / 2 - 300), (screanHeight / 2 - 175));
    }
    
    private void initComponent () {
        ln.setFont(UserProp.paneFont);
        li.setFont(UserProp.paneFont);
        name.setFont(UserProp.textFont);
        init.setFont(UserProp.textFont);
        cancel.setFont(UserProp.paneFont);
        ok.setFont(UserProp.paneFont);
        ok.setMaximumSize(cancel.getPreferredSize());
        cancel.setMaximumSize(cancel.getPreferredSize());
        init.setLineWrap(true);
        JScrollPane s = new JScrollPane(init);
        s.setPreferredSize(s.getMaximumSize());

        Box h1 = Box.createHorizontalBox();
        h1.add(Box.createHorizontalStrut(4));
        h1.add(ln);
        h1.add(Box.createHorizontalStrut(2));
        h1.add(name);
        h1.add(Box.createGlue());
        
        Box h2 = Box.createHorizontalBox();
        h2.add(Box.createHorizontalStrut(4));
        h2.add(li);
        h2.add(Box.createGlue());

        Box h3 = Box.createHorizontalBox();
        h3.add(Box.createGlue());
        h3.add(ok);
        h3.add(Box.createHorizontalStrut(4));
        h3.add(cancel);
        h3.add(Box.createHorizontalStrut(4));
        
        Box v = Box.createVerticalBox();
        v.add(h1);
        v.add(Box.createVerticalStrut(2));
        v.add(h2);
        v.add(Box.createVerticalStrut(2));
        v.add(s);
        v.add(Box.createVerticalStrut(2));

        d.add(v, BorderLayout.CENTER);
        JPanel p = new JPanel(new BorderLayout());
        p.add(h3, BorderLayout.CENTER);
        d.add(p, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent e) {
        d = new JDialog(mf, true);
        this.initFrame();
        this.initComponent();
        d.setVisible(true);
    }

}
